﻿Public Class DetalleEntregaDAO

    Public Shared Function insertarDetalleEntrega(ByVal EntregaId As Integer, ByVal ObraSocialXPlanId As Integer, ByVal DetalleEntregaObservaciones As String, ByVal CronogramaPresentacionId As Integer, ByVal DetalleEntregaAno As Integer, ByVal MesId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            Constante.esNulo(DetalleEntregaObservaciones)
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_DetalleEntrega"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.INSERTAR).DbType = DbType.Int32
                    .AddWithValue("@DetalleEntregaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@EntregaId", EntregaId).DbType = DbType.Int32
                    .AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
                    .AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
                    .AddWithValue("@DetalleEntregaObservaciones", DetalleEntregaObservaciones).DbType = DbType.String
                    .AddWithValue("@DetalleEntregaAno", DetalleEntregaAno).DbType = DbType.Int32
                    .AddWithValue("@MesId", MesId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function obtenerDetallesEntregas(ByVal DetalleEntregaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_DetalleEntrega_obtener", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@DetalleEntregaId", DetalleEntregaId).DbType = DbType.Int32
            da.Fill(ds, "DetallesEntregas")

            Return ds.Tables("DetallesEntregas")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function actualizarDetalleEntrega(ByVal DetalleEntregaId As Integer, ByVal EntregaId As Integer, ByVal ObraSocialXPlanId As Integer, ByVal DetalleEntregaObservaciones As String, ByVal CronogramaPresentacionId As Integer, ByVal DetalleEntregaAno As Integer, ByVal MesId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            Constante.esNulo(DetalleEntregaObservaciones)
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_DetalleEntrega"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ACTUALIZAR).DbType = DbType.Int32
                    .AddWithValue("@DetalleEntregaId", DetalleEntregaId).DbType = DbType.Int32
                    .AddWithValue("@EntregaId", EntregaId).DbType = DbType.Int32
                    .AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
                    .AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
                    .AddWithValue("@DetalleEntregaObservaciones", DetalleEntregaObservaciones).DbType = DbType.String
                    .AddWithValue("@DetalleEntregaAno", DetalleEntregaAno).DbType = DbType.Int32
                    .AddWithValue("@MesId", MesId).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()

            End With
            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function eliminarDetalleEntrega(ByVal DetalleEntregaId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim consulta As New SqlClient.SqlCommand
            With consulta
                .CommandText = "usp_DetalleEntrega"
                .CommandType = CommandType.StoredProcedure
                With .Parameters
                    .AddWithValue("@Accion", Constante.ELIMINAR).DbType = DbType.Int32
                    .AddWithValue("@DetalleEntregaId", DetalleEntregaId).DbType = DbType.Int32
                    .AddWithValue("@EntregaId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@CronogramaPresentacionId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@ObraSocialXPlanId", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@DetalleEntregaObservaciones", Constante.VACIO).DbType = DbType.String
                    .AddWithValue("@DetalleEntregaAno", Constante.MENOSUNO).DbType = DbType.Int32
                    .AddWithValue("@MesId", Constante.MENOSUNO).DbType = DbType.Int32
                End With
                .Connection = cn
                .ExecuteNonQuery()
            End With

            Return True
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function selectDetallesEntregas(ByVal EntregaId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_DetalleEntrega_select", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@EntregaId", EntregaId).DbType = DbType.Int32
            da.SelectCommand.Connection = cn
            da.Fill(ds, "DetallesEntregas")

            Return ds.Tables("DetallesEntregas")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function idDetalleEntrega(ByVal CronogramaPresentacionId As Integer, ByVal EntregaId As Integer, ByVal ObraSocialXPlanId As Integer, ByVal DetalleEntregaAno As Integer, ByVal MesId As Integer) As Integer
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim cmd As New SqlClient.SqlCommand("usp_detalleEntrega_id", cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
            cmd.Parameters.AddWithValue("@EntregaId", EntregaId).DbType = DbType.Int32
            cmd.Parameters.AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
            cmd.Parameters.AddWithValue("@DetalleEntregaAno", DetalleEntregaAno).DbType = DbType.Int32
            cmd.Parameters.AddWithValue("@MesId", MesId).DbType = DbType.Int32
            Dim id As Integer
            id = cmd.ExecuteScalar
            Return id
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return False
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function detallesEntregasNoCargados(ByVal CronogramaPresentacionId As Integer) As DataTable
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim ds As New DataSet
            Dim da As New SqlClient.SqlDataAdapter("usp_detalleEntrega_noCargados", cn)
            da.SelectCommand.CommandType = CommandType.StoredProcedure
            da.SelectCommand.Parameters.AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
            da.Fill(ds, "DetallesEntregas")

            Return ds.Tables("DetallesEntregas")
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function

    Public Shared Function existeDetallesEntregas(ByVal ObraSocialXPlanId As Integer, ByVal CronogramaPresentacionId As Integer, ByVal DetalleEntregaAno As Integer, ByVal MesId As Integer) As Boolean
        Dim cn As SqlClient.SqlConnection = Nothing
        Try
            cn = Conexion.Conectar
            Dim cmd As New SqlClient.SqlCommand("usp_DetalleEntrega_existe", cn)

            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@ObraSocialXPlanId", ObraSocialXPlanId).DbType = DbType.Int32
            cmd.Parameters.AddWithValue("@CronogramaPresentacionId", CronogramaPresentacionId).DbType = DbType.Int32
            cmd.Parameters.AddWithValue("@DetalleEntregaAno", DetalleEntregaAno).DbType = DbType.Int32
            cmd.Parameters.AddWithValue("@MesId", MesId).DbType = DbType.Int32


            If cmd.ExecuteScalar = 0 Then
                Return False
            Else
                Return True
            End If
        Catch ex As Exception
            'GUARDAMOS EL ERROR EN LA BD
            ErrorDAO.insertarError(ex)
            Return Nothing
        Finally
            Conexion.Desconectar(cn) 'PASAR POR PARAMETRO LA CONEXION
        End Try
    End Function
End Class
